package com.fanli.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import java.io.IOException;

/**
 * Created by liangdong.wu on 2016/8/19.
 */

public class OutcodeDecode extends UDF{
    // 转换成10进制
    public static String fn_numlto10(String str,int dec_src)
    {
        Long lg = Long.parseLong(str, dec_src);
        String str1 = lg.toString();
        return str1;
    }

    //得到trackcode
    public static String trackcode_decode_code(String lnguid){
        String str_lnguid =lnguid;
        int len_lnguid =lnguid.length();
        String trackcode ="";

        if(len_lnguid == 15){
            if(str_lnguid.substring(0,1)=="1"){
//                trackcode = substring(@str_lnguid,10,6)
                trackcode = str_lnguid.substring(9,15);
            }
            else if(str_lnguid.substring(0,1)=="2"){
//                trackcode = Char(substring(@str_lnguid,12,2))+Char(substring(@str_lnguid,14,2));
                trackcode = str_lnguid.substring(11,13) + str_lnguid.substring(13,15);
            }
        }
        else {
            trackcode = "";
        }
      return trackcode;
    }

    //得到用户ID
    public static String trackcode_decode_uid(String lnguid)
    {
        String str_lnguid =lnguid;
        int len_lnguid =lnguid.length();
        String uid ="";

        if(len_lnguid == 15){
            if(str_lnguid.substring(0,1)=="1"){
//                set @uid = cast(substring(@str_lnguid,2,8) as int);
                uid = str_lnguid.substring(1,9);
            }
            else if(str_lnguid.substring(0,1)=="2"){
                uid = str_lnguid.substring(1,11);
            }
        }
        else{
            if (Long.parseLong(lnguid) > 2147483647L){
                uid = "9";
            }
            else {
                uid = lnguid;
            }
        }
        return uid;
    }

    public static String OutcodeDecode(String outcode) throws IOException{
        String outcode_ver = outcode.substring(0, 1);
        int outcode_len = outcode.length();

        String uid ="";
        String tc ="";
        String ticket_id ="0";

        StringBuffer sb = new StringBuffer();

        if (((outcode_ver.equals("A") || outcode_ver.equals("B") || outcode_ver.equals("C")) & outcode_len==12)
                || ((outcode_ver.equals("D") || outcode_ver.equals("E") ) & outcode_len==20 )) {
            uid = fn_numlto10(outcode.substring(2, 8), 36);
            if (outcode_ver.equals("C")){
                uid = fn_numlto10(outcode.substring(1,2),36);
            }

            // tc begin
            if (outcode_ver.equals("A")){
                // set @tc=cast(substring(@outcode,9,2) as varchar(50))
                tc = outcode.substring(8,10);
            }else if(outcode_ver.equals("B") || outcode_ver.equals("C") || outcode_ver.equals("D") || outcode_ver.equals("E")){
                //set @tc=substring(@outcode,9,4)
                tc = outcode.substring(8,12);
                //System.out.println("tc is "+tc);
                if (tc.substring(0,2).toUpperCase().equals("ZZ")){
                    //set @tc=right(@tc,2)
                    tc = tc.substring(tc.length()-2, tc.length());
                }else{
                    //set @tc=left(dbo.fn_numlto10(@tc,36),6)
                    tc = fn_numlto10(tc,36).substring(0, 6);
                }

                if (outcode_ver.equals("C")){
                    //set @ticket_id=dbo.fn_numlto10(substring(@outcode,3,6),36)
                    ticket_id = fn_numlto10(outcode.substring(2,8),36);
                }
                if (outcode_ver.equals("D") || outcode_ver.equals("E") ){
                    //set @ticket_id=dbo.fn_numlto10(substring(@outcode,13,7),36)
                    ticket_id = fn_numlto10(outcode.substring(12,19),36);
                }
            }
        }
        else if(outcode_len > 15){
            tc = tc;
            uid = "3";
        }
        else if(outcode_len == 15){
            //set @tc=dbo.trackcode_decode_code(@outcode)
            //set @u_id=dbo.trackcode_decode_uid(@outcode)
            tc = trackcode_decode_code(outcode);
            uid = trackcode_decode_code(outcode);
        }else{
            boolean isNum = outcode.matches("[0-9]+");
            if(isNum){
                uid = trackcode_decode_uid(outcode);
                tc = "";
            }else {
                uid = "0";
                tc = outcode;
            }
        }
        //最后处理
        if (uid == "")
            uid = "4";
        //if isnumeric(@u_id)!=1 or PATINDEX('%[^0-9]%', @u_id)>0 set @u_id=3 --不规则
        if (outcode.matches("[^0-9]+")){
            uid = "3";
        }
        if (uid == "0") {//没登录
            uid = "1";
        }
        if (uid.length() >= 10) {
            uid = "3";
        }

        sb.append("&").append("uid=").append(uid);
        sb.append("&").append("tc=").append(tc);
        sb.append("&").append("ticket_id=").append(ticket_id);
        return sb.toString().replaceFirst("&", "");
    }

    public String evaluate(String str) throws IOException{
        return OutcodeDecode(str);
    }
    public static void main(String[] argvs) throws IOException{
        System.out.println(OutcodeDecode("D01ee0yyZZ002noa4474"));
    }
}